"""
系统设置对话框
"""

from PyQt5.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QLabel, QPushButton,
    QTabWidget, QWidget, QFormLayout, QLineEdit, QComboBox,
    QSpinBox, QCheckBox, QGroupBox, QFileDialog
)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont

class SettingsDialog(QDialog):
    """系统设置对话框"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        
        self.setWindowTitle("⚙️ 系统设置")
        self.setFixedSize(600, 500)
        self.setModal(True)
        
        self.setup_ui()
        self.setup_style()
        
        # 加载设置
        self.load_settings()
    
    def setup_ui(self):
        """设置用户界面"""
        layout = QVBoxLayout(self)
        layout.setContentsMargins(20, 20, 20, 20)
        layout.setSpacing(16)
        
        # 标题
        title_label = QLabel("⚙️ 系统设置")
        title_label.setFont(QFont("Microsoft YaHei", 14, QFont.Bold))
        title_label.setStyleSheet("color: #1976d2; margin-bottom: 10px;")
        layout.addWidget(title_label)
        
        # 标签页控件
        self.tab_widget = QTabWidget()
        
        # 数据库设置标签页
        self.create_database_tab()
        
        # 界面设置标签页
        self.create_ui_tab()
        
        # 系统设置标签页
        self.create_system_tab()
        
        layout.addWidget(self.tab_widget)
        
        # 按钮区域
        button_layout = QHBoxLayout()
        button_layout.addStretch()
        
        # 重置按钮
        reset_button = QPushButton("重置")
        reset_button.setFixedSize(80, 32)
        reset_button.clicked.connect(self.reset_settings)
        button_layout.addWidget(reset_button)
        
        # 取消按钮
        cancel_button = QPushButton("取消")
        cancel_button.setFixedSize(80, 32)
        cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(cancel_button)
        
        # 确定按钮
        ok_button = QPushButton("确定")
        ok_button.setFixedSize(80, 32)
        ok_button.clicked.connect(self.save_settings)
        button_layout.addWidget(ok_button)
        
        layout.addLayout(button_layout)
    
    def create_database_tab(self):
        """创建数据库设置标签页"""
        db_widget = QWidget()
        db_layout = QVBoxLayout(db_widget)
        db_layout.setSpacing(16)
        
        # 数据库连接设置组
        db_group = QGroupBox("数据库连接")
        db_group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        db_form = QFormLayout(db_group)
        
        # 数据库路径
        self.db_path_edit = QLineEdit()
        self.db_path_edit.setPlaceholderText("数据库文件路径")
        db_form.addRow("数据库路径:", self.db_path_edit)
        
        # 浏览按钮
        browse_button = QPushButton("浏览...")
        browse_button.clicked.connect(self.browse_database)
        db_form.addRow("", browse_button)
        
        # 连接超时
        self.timeout_spin = QSpinBox()
        self.timeout_spin.setRange(1, 60)
        self.timeout_spin.setValue(30)
        self.timeout_spin.setSuffix(" 秒")
        db_form.addRow("连接超时:", self.timeout_spin)
        
        # 自动清理
        self.auto_cleanup_check = QCheckBox("启用自动清理")
        self.auto_cleanup_check.setChecked(True)
        db_form.addRow("", self.auto_cleanup_check)
        
        db_layout.addWidget(db_group)
        
        # 备份设置组
        backup_group = QGroupBox("备份设置")
        backup_group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        backup_form = QFormLayout(backup_group)
        
        # 自动备份
        self.auto_backup_check = QCheckBox("启用自动备份")
        backup_form.addRow("", self.auto_backup_check)
        
        # 备份间隔
        self.backup_interval_spin = QSpinBox()
        self.backup_interval_spin.setRange(1, 30)
        self.backup_interval_spin.setValue(7)
        self.backup_interval_spin.setSuffix(" 天")
        backup_form.addRow("备份间隔:", self.backup_interval_spin)
        
        # 备份路径
        self.backup_path_edit = QLineEdit()
        self.backup_path_edit.setPlaceholderText("备份文件路径")
        backup_form.addRow("备份路径:", self.backup_path_edit)
        
        db_layout.addWidget(backup_group)
        db_layout.addStretch()
        
        self.tab_widget.addTab(db_widget, "🗄️ 数据库")
    
    def create_ui_tab(self):
        """创建界面设置标签页"""
        ui_widget = QWidget()
        ui_layout = QVBoxLayout(ui_widget)
        ui_layout.setSpacing(16)
        
        # 外观设置组
        appearance_group = QGroupBox("外观设置")
        appearance_group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        appearance_form = QFormLayout(appearance_group)
        
        # 主题
        self.theme_combo = QComboBox()
        self.theme_combo.addItems(["默认", "深色", "浅色"])
        appearance_form.addRow("主题:", self.theme_combo)
        
        # 字体大小
        self.font_size_spin = QSpinBox()
        self.font_size_spin.setRange(8, 20)
        self.font_size_spin.setValue(10)
        appearance_form.addRow("字体大小:", self.font_size_spin)
        
        # 语言
        self.language_combo = QComboBox()
        self.language_combo.addItems(["中文简体", "English"])
        appearance_form.addRow("语言:", self.language_combo)
        
        ui_layout.addWidget(appearance_group)
        
        # 行为设置组
        behavior_group = QGroupBox("行为设置")
        behavior_group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        behavior_form = QFormLayout(behavior_group)
        
        # 记住窗口状态
        self.remember_state_check = QCheckBox("记住窗口状态和大小")
        self.remember_state_check.setChecked(True)
        behavior_form.addRow("", self.remember_state_check)
        
        # 自动保存
        self.auto_save_check = QCheckBox("自动保存更改")
        self.auto_save_check.setChecked(True)
        behavior_form.addRow("", self.auto_save_check)
        
        # 确认对话框
        self.confirm_dialog_check = QCheckBox("显示确认对话框")
        self.confirm_dialog_check.setChecked(True)
        behavior_form.addRow("", self.confirm_dialog_check)
        
        ui_layout.addWidget(behavior_group)
        ui_layout.addStretch()
        
        self.tab_widget.addTab(ui_widget, "🎨 界面")
    
    def create_system_tab(self):
        """创建系统设置标签页"""
        system_widget = QWidget()
        system_layout = QVBoxLayout(system_widget)
        system_layout.setSpacing(16)
        
        # 日志设置组
        log_group = QGroupBox("日志设置")
        log_group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        log_form = QFormLayout(log_group)
        
        # 日志级别
        self.log_level_combo = QComboBox()
        self.log_level_combo.addItems(["DEBUG", "INFO", "WARNING", "ERROR"])
        self.log_level_combo.setCurrentText("INFO")
        log_form.addRow("日志级别:", self.log_level_combo)
        
        # 日志文件大小限制
        self.log_size_spin = QSpinBox()
        self.log_size_spin.setRange(1, 100)
        self.log_size_spin.setValue(10)
        self.log_size_spin.setSuffix(" MB")
        log_form.addRow("文件大小限制:", self.log_size_spin)
        
        # 日志保留天数
        self.log_days_spin = QSpinBox()
        self.log_days_spin.setRange(1, 365)
        self.log_days_spin.setValue(30)
        self.log_days_spin.setSuffix(" 天")
        log_form.addRow("保留天数:", self.log_days_spin)
        
        system_layout.addWidget(log_group)
        
        # 性能设置组
        performance_group = QGroupBox("性能设置")
        performance_group.setFont(QFont("Microsoft YaHei", 10, QFont.Bold))
        performance_form = QFormLayout(performance_group)
        
        # 缓存大小
        self.cache_size_spin = QSpinBox()
        self.cache_size_spin.setRange(10, 1000)
        self.cache_size_spin.setValue(100)
        self.cache_size_spin.setSuffix(" MB")
        performance_form.addRow("缓存大小:", self.cache_size_spin)
        
        # 查询限制
        self.query_limit_spin = QSpinBox()
        self.query_limit_spin.setRange(100, 10000)
        self.query_limit_spin.setValue(1000)
        performance_form.addRow("查询限制:", self.query_limit_spin)
        
        system_layout.addWidget(performance_group)
        system_layout.addStretch()
        
        self.tab_widget.addTab(system_widget, "⚙️ 系统")
    
    def setup_style(self):
        """设置样式"""
        self.setStyleSheet("""
            QDialog {
                background-color: #ffffff;
            }
            QTabWidget::pane {
                border: 1px solid #e0e0e0;
                background-color: #ffffff;
                border-radius: 4px;
            }
            QTabBar::tab {
                background-color: #f5f5f5;
                padding: 10px 20px;
                margin-right: 2px;
                border-top-left-radius: 4px;
                border-top-right-radius: 4px;
            }
            QTabBar::tab:selected {
                background-color: #ffffff;
                border-bottom: 2px solid #1976d2;
            }
            QGroupBox {
                font-weight: bold;
                border: 2px solid #e0e0e0;
                border-radius: 5px;
                margin-top: 10px;
                padding-top: 10px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
            }
            QPushButton {
                background-color: #1976d2;
                color: white;
                border: none;
                border-radius: 4px;
                font-weight: bold;
                padding: 6px 12px;
            }
            QPushButton:hover {
                background-color: #1565c0;
            }
            QPushButton:pressed {
                background-color: #0d47a1;
            }
            QLineEdit, QComboBox, QSpinBox {
                padding: 6px 12px;
                border: 1px solid #e0e0e0;
                border-radius: 4px;
                background-color: #ffffff;
            }
            QLineEdit:focus, QComboBox:focus, QSpinBox:focus {
                border-color: #1976d2;
            }
        """)
    
    def browse_database(self):
        """浏览数据库文件"""
        file_path, _ = QFileDialog.getOpenFileName(
            self, "选择数据库文件", "", "SQLite数据库 (*.db);;所有文件 (*.*)"
        )
        if file_path:
            self.db_path_edit.setText(file_path)
    
    def load_settings(self):
        """加载设置（占位符实现）"""
        # TODO: 从配置文件加载设置
        pass
    
    def save_settings(self):
        """保存设置（占位符实现）"""
        # TODO: 保存设置到配置文件
        self.accept()
    
    def reset_settings(self):
        """重置设置（占位符实现）"""
        # TODO: 重置为默认设置
        self.load_settings()